#!/usr/env python

import csv
from datetime import datetime

def write_doc_comment(test_file, author, date, case_id, purpose, priority):
    test_file.write("/**\n")
    test_file.write(" * @author: %s\n" % author)
    test_file.write(" * @date: %s\n" % date.isoformat())
    test_file.write(" * @version: 0.1\n")
    test_file.write(" * @TCID: %s\n" % case_id)
    test_file.write(" * Purpose: test %s\n" % purpose)
    test_file.write(" * Priority: %s\n" % priority)
    test_file.write(" */\n")

def get_year_test(test_file, test_name, year, expected):
    test_file.write("@Test public void %s() {\n" % test_name)
    test_file.write("    Year year = new Year(%s);\n" % year)
    test_file.write("    assertEquals(%s, year.getYear());\n" % expected)
    test_file.write("}\n\n")

def increment_test(test_file, test_name, year, expected):
    #print test_name
    test_file.write("@Test public void %s() {\n" % test_name)
    test_file.write("    Year year = new Year(%s);\n" % year)
    test_file.write("    boolean result = year.increment();\n")

    exp_result, exp_pos = expected.split('/')
    if exp_result == 'true':
        test_file.write("    assertTrue(result);\n")
    else:
        test_file.write("    assertFalse(result);\n")
    test_file.write("    assertEquals(%s, year.getYear());\n" % exp_pos)
    test_file.write("}\n\n")

def isleap_test(test_file, test_name, year, expected):
    test_file.write("@Test public void %s() {\n" % test_name)
    test_file.write("    Year year = new Year(%s);\n" % year)
    if expected == 'false':
        test_file.write("    assertFalse(year.isLeap());\n")
    else:
        test_file.write("    assertTrue(year.isLeap());\n")
    test_file.write("}\n\n")

with open('year_test_design.csv') as csv_file:
    reader = csv.reader(csv_file)
    reader.next() # skip title line
    
    header = reader.next()
    case_idx = header.index('Test Case')
    name_idx = header.index('Name')
    y_idx = header.index('Year')
    exp_idx = header.index('Expected')
    pps_idx = header.index('Purpose')
    prt_idx = header.index('Priority')
    

    today = datetime.today()
    with open('year_test_methods.java', 'w') as test_file:
        for row in reader:
            test_case = row[case_idx]
            test_name = row[name_idx]
            y = row[y_idx]
            exp = row[exp_idx]
            purpose = row[pps_idx]
            priority = row[prt_idx]

            write_doc_comment(test_file, "kavinyao", today, test_case, purpose, priority)
            if "getYearTest" in test_name:
                get_year_test(test_file, test_name, y, exp)
            elif "incrementTest" in test_name:
                increment_test(test_file, test_name, y, exp)
            elif "isLeapTest" in test_name:
                isleap_test(test_file, test_name, y, exp)
